//
//  BLCommonTool.m
//  Byelone
//
//  Created by 乔小乔 on 2021/1/31.
//

#import "BLCommonTool.h"

@implementation BLCommonTool





+(UIImage*)imageWithImageSimple:(UIImage*)image{
    CGImageRef imgRef = image.CGImage;
    CGFloat width = CGImageGetWidth(imgRef);
    CGFloat height = CGImageGetHeight(imgRef);
    CGAffineTransform transform = CGAffineTransformIdentity;
    CGRect bounds = CGRectMake(0, 0, width, height);
    CGFloat scaleRatio = 1;
    CGFloat boundHeight;
    UIImageOrientation orient = image.imageOrientation;
    switch(orient){
        case UIImageOrientationUp: //EXIF = 1
        {
            transform = CGAffineTransformIdentity;
            break;
        }
        case UIImageOrientationUpMirrored: //EXIF = 2
        {
            transform = CGAffineTransformMakeTranslation(width, 0.0);
            transform = CGAffineTransformScale(transform, -1.0, 1.0);
            break;
        }
        case UIImageOrientationDown: //EXIF = 3
        {
            transform = CGAffineTransformMakeTranslation(width, height);
            transform = CGAffineTransformRotate(transform, M_PI);
            break;
        }
        case UIImageOrientationDownMirrored: //EXIF = 4
        {
            transform = CGAffineTransformMakeTranslation(0.0, height);
            transform = CGAffineTransformScale(transform, 1.0, -1.0);
            break;
        }
        case UIImageOrientationLeftMirrored: //EXIF = 5
        {
            boundHeight = bounds.size.height;
            bounds.size.height = bounds.size.width;
            bounds.size.width = boundHeight;
            transform = CGAffineTransformMakeTranslation(height, width);
            transform = CGAffineTransformScale(transform, -1.0, 1.0);
            transform = CGAffineTransformRotate(transform, 3.0 * M_PI / 2.0);
            break;
        }
        case UIImageOrientationLeft: //EXIF = 6
        {
            boundHeight = bounds.size.height;
            bounds.size.height = bounds.size.width;
            bounds.size.width = boundHeight;
            transform = CGAffineTransformMakeTranslation(0.0, width);
            transform = CGAffineTransformRotate(transform, 3.0 * M_PI / 2.0);
            break;
        }
        case UIImageOrientationRightMirrored: //EXIF = 7
        {
            boundHeight = bounds.size.height;
            bounds.size.height = bounds.size.width;
            bounds.size.width = boundHeight;
            transform = CGAffineTransformMakeScale(-1.0, 1.0);
            transform = CGAffineTransformRotate(transform, M_PI / 2.0);
            break;
        }
        case UIImageOrientationRight: //EXIF = 8
        {
            boundHeight = bounds.size.height;
            bounds.size.height = bounds.size.width;
            bounds.size.width = boundHeight;
            transform = CGAffineTransformMakeTranslation(height, 0.0);
            transform = CGAffineTransformRotate(transform, M_PI / 2.0);
            break;
        }
        default:
        {
            break;
        }
    }
    UIGraphicsBeginImageContext(bounds.size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    if(orient == UIImageOrientationRight || orient == UIImageOrientationLeft){
        CGContextScaleCTM(context, -scaleRatio, scaleRatio);
        CGContextTranslateCTM(context, -height, 0);
    }else{
        CGContextScaleCTM(context, scaleRatio, -scaleRatio);
        CGContextTranslateCTM(context, 0, -height);
    }
    CGContextConcatCTM(context, transform);
    CGContextDrawImage(UIGraphicsGetCurrentContext(), CGRectMake(0, 0, width, height), imgRef);
    UIImage *imageCopy = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    
    CGImageRef imgRef2 = imageCopy.CGImage;
    CGFloat width2 = CGImageGetWidth(imgRef2);
    CGFloat height2 = CGImageGetHeight(imgRef2);
    
    
    if(width2 > 1024){ //等比缩放图片
        CGFloat n_width2 = 1024.0f;
        CGFloat n_height2 = 1024.0f*height2/width2;
        UIGraphicsBeginImageContext(CGSizeMake(n_width2, n_height2));
        [imageCopy drawInRect:CGRectMake(0,0,n_width2,n_height2)];
        UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
        return newImage;
    }else{
        return imageCopy;
    }
}

+ (UIView *)createTip:(UIImage *)img title:(NSString *)title color:(UIColor *)color point:(CGPoint)point{
    UIView *view = [[UIView alloc]initWithFrame:CGRectMake(point.x, point.y, 0, 14)];
    view.backgroundColor = color;
    view.layer.cornerRadius = 7;
    view.layer.masksToBounds = YES;
    
    CGFloat space = 11.5;
    if (img) {
        space = 17;
        UIImageView *imgView = [[UIImageView alloc]initWithFrame:CGRectMake(5, 3, 8, 8)];
        imgView.contentMode = UIViewContentModeScaleAspectFit;
        imgView.image = img;
        [view addSubview:imgView];
    }
    UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(space, 0, 0, 14)];
    label.font = [UIFont systemFontOfSize:9 weight:UIFontWeightMedium];
    label.textColor = [UIColor whiteColor];
    label.text = title;
    [label sizeToFit];
    CGSize labelSize = label.frame.size;
    label.frame = CGRectMake(space, 0, labelSize.width, 14);
    [view addSubview:label];
    view.frame = CGRectMake(point.x, point.y, labelSize.width+23, 14);
                  
    return view;
}
+ (UIView *)createDesc:(UIImage *)img title:(NSString *)title color:(UIColor *)color point:(CGPoint)point{
    UIView *view = [[UIView alloc]initWithFrame:CGRectMake(point.x, point.y, 0, 14)];
//    view.backgroundColor = color;
//    view.layer.cornerRadius = 7;
//    view.layer.masksToBounds = YES;
    
    CGFloat space = 0;
    if (img) {
        space = 12;
        UIImageView *imgView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 2, 10, 10)];
        imgView.contentMode = UIViewContentModeScaleAspectFit;
        imgView.image = img;
        [view addSubview:imgView];
    }
    UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(space, 0, 0, 14)];
    label.font = [UIFont systemFontOfSize:9 weight:UIFontWeightMedium];
    label.textColor = color;
    label.text = title;
    [label sizeToFit];
    CGSize labelSize = label.frame.size;
    label.frame = CGRectMake(space, 0, labelSize.width, 14);
    [view addSubview:label];
    view.frame = CGRectMake(point.x, point.y, labelSize.width+12, 14);
                  
    return view;
}
@end
